<template>
  <div class="login-container">
    <div class="center">
      <div class="login-banner"/>
      <div class="login-wrapper">
        <div class="qrcode"/>
        <i class="code" @click="change"/>
        <el-tabs v-model="activeName" stretch @tab-click="handleClick">
          <el-tab-pane label="账户登录" name="first">
            <el-form ref="loginForm" :model="loginForm" class="login-form" :rules="loginRules">
              <el-form-item prop="username">
                <i class="el-icon-user icon"/>
                <el-input v-model="loginForm.username" placeholder="请输入用户名" @keyup.enter.native="handleLogin"/>
              </el-form-item>
              <el-form-item prop="password">
                <i class="el-icon-lock icon"/>
                <el-input v-model="loginForm.password" type="password" placeholder="请输入密码"
                          @keyup.enter.native="handleLogin"
                />
              </el-form-item>
              <el-form-item style="margin-bottom: 0">
                <el-checkbox v-model="checked">记住密码</el-checkbox>
              </el-form-item>
              <el-form-item>
                <el-button :loading="loading"
                           @click.native.prevent="handleLogin"
                >登录
                </el-button>
              </el-form-item>
            </el-form>
            <div class="bottom">登录即代表您已阅读并同意
              <router-link to="/agreement"><span>《隐私协议和服务声明》</span></router-link>
            </div>
          </el-tab-pane>
          <el-tab-pane label="手机登录" name="second">
            <el-form :data="form" class="login-form">
              <el-form-item>
                <i class="el-icon-user icon"/>
                <el-input v-model="form.username" placeholder="请输入手机号" disabled/>
              </el-form-item>
              <el-form-item>
                <i class="el-icon-lock icon"/>
                <el-input v-model="form.verify" placeholder="请输入验证码" disabled>
                  <span slot="append" @click="send">{{ tip }}</span>
                </el-input>
              </el-form-item>
              <el-form-item style="height: 18px"/>
              <el-form-item>
                <el-button style="width: 100%; background-color:#1AA97B; color:aliceblue;" @click="phoneSubmit">登录
                </el-button>
              </el-form-item>
            </el-form>
            <div class="bottom">登录即代表您已阅读并同意
              <router-link to="/agreement"><span>《隐私协议和服务声明》</span></router-link>
            </div>
          </el-tab-pane>
        </el-tabs>
        <div v-show="show" class="show-qrcode">
          <div class="header" @click="show = false">返回账号登录</div>
          <div class="img">
            <!-- 二维码 -->
            <img src="../../assets/qrcode.png" alt="">
          </div>
        </div>
      </div>
    </div>
    <div class="square">
      <ul>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
      </ul>
    </div>
    <div class="circle">
      <ul>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
      </ul>
    </div>
  </div>
</template>

<script>
import { getAccount, isCheck } from '@/utils/account'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length === 0) {
        callback(new Error('请输入密码'))
      } else if (value.length < 8) {
        callback(new Error('密码不得少于8位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      form: {},
      loginRules: {
        username: [{ required: true, validator: validateUsername }],
        password: [{ required: true, validator: validatePassword }]
      },
      checked: false,
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      activeName: 'first',
      tip: '发送验证码',
      flag: true,
      show: false
    }
  },
  computed: {
    year() {
      return new Date().getFullYear()
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  mounted() {
    this.handleClick()
  },
  methods: {
    handleClick() {
      if (this.activeName === 'first' && this.show === false) {
        this.checked = isCheck()
        if (this.checked) {
          this.loginForm = Object.assign({}, getAccount())
        }
      }
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      if (this.loginForm.username === 'admin' && this.loginForm.password === '12345678') {
        localStorage.setItem('token','token')
        this.$router.push({ path: this.redirect || '/' })
      }
    },
    // handleLogin() {
    //   this.$refs.loginForm.validate(valid => {
    //     if (valid) {
    //       this.loading = true
    //       this.$store.dispatch('user/login', this.loginForm).then(() => {
    //         if (this.checked) {
    //           setAccount(this.loginForm)
    //           setCheck()
    //         } else {
    //           rmAccount()
    //           rmCheck()
    //         }
    //         setAccount(this.loginForm)
    //         this.$router.push({ path: this.redirect || '/' })
    //         this.loading = false
    //       }).catch(() => {
    //         this.loading = false
    //       })
    //     } else {
    //       return false
    //     }
    //   })
    // },
    change() {
      this.$notify({
        title: '警告',
        message: '此功能暂未开通',
        type: 'warning'
      })
      this.show = false
    },
    phoneSubmit() {
      this.$notify({
        title: '警告',
        message: '此功能暂未开通',
        type: 'warning'
      })
    },
    send() {
      if (this.flag) {
        this.$notify({
          title: '警告',
          message: '此功能暂未开通',
          type: 'warning'
        })
        return false
      }
      this.flag = true
      let time = 10
      let timer = null
      const _that = this
      timer = setInterval(function() {
        time -= 1
        _that.tip = `${time}秒重新获取`
        if (time === 0) {
          _that.flag = false
          _that.tip = '发送验证码'
          clearInterval(timer)
        }
      }, 1000)
    }
  }
}
</script>

<style lang="scss">
.login-container {
  height: 100%;
  min-height: 550px;
  position: relative;
  /* 渐变背景 */
  background: linear-gradient(200deg, #0ce59f, #08a774);
  /* 溢出隐藏 */
  overflow: hidden;

  .center {
    width: 1000px;
    position: absolute;
    top: 150px;
    left: 50%;
    margin-left: -500px;

    .login-banner {
      width: 500px;
      height: 400px;
      background-size: 100%;
      float: left;
    }

    .login-wrapper {
      float: right;
      width: 400px;
      background-color: #FFFFFF;
      border-radius: 10px;
      padding: 10px 40px 10px;
      overflow: hidden;
      box-shadow: 0 0 20px #E0E0E0;
      position: relative;

      .code {
        display: block;
        width: 30px;
        height: 30px;
        // 二维码
        background: url('../../assets/qrcode.png') no-repeat;
        background-size: 100% 100%;
        position: absolute;
        right: 5px;
        top: 5px;
        cursor: pointer;
      }

      .qrcode {
        width: 0;
        height: 0;
        border: 45px solid transparent;
        border-left-color: #ededed;
        position: absolute;
        transform: rotate(-45deg);
        right: -45px;
        top: -45px;
      }
    }
  }
  .el-tabs__active-bar{
    background-color: #0a986a;
  }
  .el-button--default{
    width: 100%;
    background: linear-gradient(200deg, #0ce59f, #08a774);
    color: #fff;
    /* 动画过渡 */
    transition: 1s;
  }
  .el-button--default:hover{
    background: linear-gradient(200deg, #08a774, #0ce59f);
    background-position-x: -320px;
  }
  .el-tabs__item {
    color: #999999;
    font-size: 18px;
  }

  .el-tabs__item.is-active {
    color: #000000;
  }

  .el-tabs__active-bar {
    height: 3px;
  }

  .login-form {
    margin-top: 20px;

    .el-form-item {
      position: relative;

      .el-input__inner {
        padding-left: 40px;
        box-sizing: border-box;
      }

      .icon {
        position: absolute;
        z-index: 2;
        font-size: 20px;
        top: 8px;
        left: 10px;
        color: #666666;
      }
    }

  }

  .flex {
    display: flex;
    flex-direction: space-around
  }

  .el-input-group__append {
    width: 100px;
    text-align: center;
    cursor: pointer;
  }

  .show-qrcode {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background: #FFFFFF;
    z-index: 3
  }

  .show-qrcode {
    .header {
      float: right;
      font-size: 14px;
      margin: 12px;
    }

    .img {
      width: 170px;
      height: 170px;
      position: absolute;
      left: 50%;
      margin-left: -85px;
      top: 50%;
      margin-top: -85px;

      img {
        width: 100%;
        height: 100%;
      }
    }
  }


  .bottom {
    font-size: 13px;
    text-align: center;
    margin-bottom: 20px;

    span {
      color: #066748;
    }
  }

  ul li {
    position: absolute;
    border: 1px solid #fff;
    background-color: #fff;
    width: 30px;
    height: 30px;
    list-style: none;
    opacity: 0;
  }

  .square {
    li {
      top: 40vh;
      left: 60vw;
      /* 执行动画：动画名 时长 线性的 无限次播放 */
      animation: square 10s linear infinite;
    }

    li:nth-child(2) {
      top: 80vh;
      left: 10vw;
      /* 设置动画延迟时间 */
      animation-delay: 2s;
    }

    li:nth-child(3) {
      top: 80vh;
      left: 85vw;
      /* 设置动画延迟时间 */
      animation-delay: 4s;
    }

    li:nth-child(4) {
      top: 10vh;
      left: 70vw;
      /* 设置动画延迟时间 */
      animation-delay: 6s;
    }

    li:nth-child(5) {
      top: 10vh;
      left: 10vw;
      /* 设置动画延迟时间 */
      animation-delay: 8s;
    }
  }

  .circle {
    li {
      bottom: 0;
      left: 15vw;
      /* 执行动画 */
      animation: circle 10s linear infinite;
    }

    li:nth-child(2) {
      left: 35vw;
      /* 设置动画延迟时间 */
      animation-delay: 2s;
    }

    li:nth-child(3) {
      left: 55vw;
      /* 设置动画延迟时间 */
      animation-delay: 6s;
    }

    li:nth-child(4) {
      left: 75vw;
      /* 设置动画延迟时间 */
      animation-delay: 4s;
    }

    li:nth-child(5) {
      left: 90vw;
      /* 设置动画延迟时间 */
      animation-delay: 8s;
    }
  }

  /* 定义动画 */
  @keyframes square {
    0% {
      transform: scale(0) rotateY(0deg);
      opacity: 1;
    }
    100% {
      transform: scale(5) rotateY(1000deg);
      opacity: 0;
    }
  }
  @keyframes circle {
    0% {
      transform: scale(0) rotateY(0deg);
      opacity: 1;
      bottom: 0;
      border-radius: 0;
    }
    100% {
      transform: scale(5) rotateY(1000deg);
      opacity: 0;
      bottom: 90vh;
      border-radius: 50%;
    }
  }
}
</style>
